import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/yaoHome.vue'),
    meta: {
      navBarShow: true
    }
  },
  {
    path: '/category',
    name: 'category',
    component: () => import('@/views/category/yaoCategory.vue'),
    meta: {
      navBarShow: true
    }
  },
  {
    path: '/Search/:goodsName',
    name: 'Search',
    component: () => import('@/views/search/yaoSearch.vue'),
    meta: {
      navBarShow: false
    }
  },
  {
    path: '/SearchNoKeywords',
    name: 'SearchNoKeywords',
    component: () => import('@/views/search/yaoSearchNoKeywords.vue'),
    meta: {
      navBarShow: false
    }
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('@/views/cart/yaoCart.vue'),
    meta: {
      navBarShow: true
    }
  },
  {
    path: '/center',
    name: 'center',
    component: () => import('@/views/center/yaoCenter.vue'),
    meta: {
      navBarShow: true
    }
  },
  {
    path: '/product',
    name: 'product',
    component: () => import('@/views/product/yaoProduct.vue'),
    meta: {
      navBarShow: false
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/yaoLogin.vue'),
    meta: {
      navBarShow: false
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/login/yaoRegister.vue'),
    meta: {
      navBarShow: false
    }
  },
  {
    path: '*',
    redirect: '/home'
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 登录拦击
router.beforeEach((to, from, next) => {
  //验证token ,只有存在token的时候才能跳转到内容页面
  let token = localStorage.getItem('token');
  if (token || to.path === '/login' || to.path == '/register') {
    next()
  } else {
    next('/login')
  }
})

export default router
